DEW.HomeController = function() {};

$.extend(DEW.HomeController.prototype, {
    render: function() {
        DEW.ViewManager.removeAll();
        this.displayLayout();
        this.displayHeader();
        this.displayBody();
        this.displayFooter();
    },

    displayLayout: function() {
        var baseLayout = new DEW.BaseLayout();
        baseLayout.render();
        $(baseLayout.el).appendTo("#content");
        DEW.ViewManager.addView(baseLayout);
    },

    displayHeader: function() {
        var header = new DEW.Header({
            logo: {
                src: 'images/logo.jpg',
                alt: 'Logo'
            },
            enlaces: [{
                id: 0,
                text: 'Iniciar Sesión',
                show: true,
                textonly: false,
                handler: function() {
                    var loginPopup = new DEW.PopupWindow({
                        ctClass: 'loginPopup',
                        title: 'Iniciar Sesión',
                        needValidation: false,
                        panel: new DEW.Login({
                            model: new DEW.LoginModel()
                        }),
                        buttons: [{
                            id: 'create-btn',
                            name : 'Ingresar',
                            hidden: false,
                            primary: true,
                            handler: function() {
                                loginPopup.panel.authenticate({
                                    success: function(email) {
                                        loginPopup.panel.hideLoginError();
                                        header.login(email);
                                        loginPopup.close();
                                    }
                                });
                            }
                        }]
                    });
                    loginPopup.render();
                    $("body").append(loginPopup.el);
                }
            },{
                id: 1,
                text: '',
                textonly: true,
                show: false
            },{
                id: 2,
                text: 'Salir',
                show: false,
                textonly: false,
                handler: function() {
                    header.logout();
                }
            }]
        });
        header.render();
        $(header.el).appendTo("#header");
        DEW.ViewManager.addView(header);
    },

    displayBody: function() {

        //Cargando categorias
        var categoriesCollection = new DEW.CategoriesCollection();
        categoriesCollection.fetch({reset: true});

        var sidebar = new DEW.Sidebar({
            model: categoriesCollection
        });

        sidebar.on("changeCategory",function(categoryId) {
            listView.categoryId = categoryId;
            listView.render();
        });

        $(sidebar.el).appendTo(".sidebar");
        DEW.ViewManager.addView(sidebar);

        //Cargando lista de datos
        var listCollection = DEW.StoreManager.get('listCollection');
        if (listCollection === null) {
            listCollection = new DEW.ListCollection();
            listCollection.fetch({reset:true});
        }

        //Creando la vista y asociandole el modelo
        var listView = new DEW.List({
            model: listCollection
        });
        $(listView.el).appendTo(".page-content");
        DEW.ViewManager.addView(listView);
    },

    displayFooter: function() {
        var footer = new DEW.Footer();
        footer.render();
        $(footer.el).appendTo("#footer");
        DEW.ViewManager.addView(footer);
    }

});